const app = getApp();
Page({

    // 页面的初始数据
    data: {
        current:0,
        page:1,
        pagesize:5,
        orderlist:[],
        status:'-2,-1,1,2,3,4'
    },

    // 页面的生命周期函数 – 监听页面加载
    onLoad(res) {
        if(res.idx){
            this.headerHandle_onload(res.idx)
        }else{
           this.getorderDataMax('-2,-1,1,2,3,4',this.data.page,999) 
        }
    },
    
    getorderData(status,page,pagesize){
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'listOrder',
            data:{
                mid:app.globalData.userInfo.mid,
                status,
                page,
                pagesize
            },
            success(e){
                // console.log(e.data)
                var obj = e.data;
                for(var o of obj){
                    var total_num = 0;
                    for(var i of o.goods){
                        total_num = parseInt(total_num) + parseInt(i.shop_num)
                    }
                    o.total_num = total_num
                }
                // console.log(obj)
                that.setData({
                    orderlist:obj
                })
            }
        });
    },

    getorderDataMax(status,page,pagesize){
        const that = this;
        swan.request({
            url: app.globalData.urlapi + 'listOrder',
            data:{
                mid:app.globalData.userInfo.mid,
                status,
                page,
                pagesize
            },
            success(e){
                that.setData({
                    status
                },function setpage(){
                    if(that.selectComponent('#page')==undefined){
                        setTimeout(setpage, 500);
                    }else{
                        that.selectComponent('#page').change_rellist(e.data.length,that.data.pagesize)
                    }
                })
            }
        });
    },

    getpage(e){
        this.getorderData(this.data.status,e.detail,this.data.pagesize)
        // console.log(e.detail)
    },

    headerHandle(e){
        switch(e.currentTarget.dataset.idx){
            case '0':
                this.getorderDataMax('-2,-1,1,2,3,4',this.data.page,999)
            break;
            case '1':
                this.getorderDataMax('1',this.data.page,999)
            break;
            case '2':
                this.getorderDataMax('2',this.data.page,999)
            break;
            case '3':
                this.getorderDataMax('3',this.data.page,999)
            break;
            case '4':
                this.getorderDataMax('4',this.data.page,999)
            break;
            case '5':
                this.getorderDataMax('-1',this.data.page,999)
            break;
            case '6':
                this.getorderDataMax('-2',this.data.page,999)
            break;
        }
        this.setData({
            current:e.currentTarget.dataset.idx
        })
    },

    headerHandle_onload(e){
        switch(e){
            case '0':
                this.getorderDataMax('-2,-1,1,2,3,4',this.data.page,999)
            break;
            case '1':
                this.getorderDataMax('1',this.data.page,999)
            break;
            case '2':
                this.getorderDataMax('2',this.data.page,999)
            break;
            case '3':
                this.getorderDataMax('3',this.data.page,999)
            break;
            case '4':
                this.getorderDataMax('4',this.data.page,999)
            break;
            case '5':
                this.getorderDataMax('-1',this.data.page,999)
            break;
            case '6':
                this.getorderDataMax('-2',this.data.page,999)
            break;
        }
        this.setData({
            current:e
        })
    },

    godetail(e){
        swan.navigateTo({
            url: '../order_detail/order_detail?id='+e.currentTarget.dataset.id+'&order_id='+e.currentTarget.dataset.order_id,
        });
    },

    go_pay(e){
        swan.navigateTo({
            url: '../order_success/order_success?order_id='+e.currentTarget.dataset.order_id
        });
    },

    // 页面的生命周期函数 – 监听页面初次渲染完成
    onReady(res) {

    },

    // 页面的生命周期函数 – 监听页面显示
    onShow(res) {

    },

    // 页面的生命周期函数 – 监听页面隐藏
    onHide(res) {

    },

    // 页面的生命周期函数 – 监听页面卸载
    onUnload(res) {

    },

    // 页面的生命周期函数 – 监听页面重启，单击重启按钮时触发
    onForceReLaunch(res) {

    },

    // 页面的事件处理函数 – 监听用户下拉动作
    onPullDownRefresh(res) {

    },

    // 页面的事件处理函数 – 上拉触底事件的处理函数
    onReachBottom(res) {

    },

    // 页面的事件处理函数 – 用户点击右上角转发
    onShareAppMessage(res) {

    },

    // 页面的事件处理函数 – 页面滚动触发事件的处理函数
    onPageScroll(res) {

    },

    // 页面的事件处理函数 – 当前是 tab 页时，点击 tab 时触发
    onTabItemTap(res) {

    }
});